package com.keba.lamp.online.service.impl;

import cn.dev33.satoken.sign.template.SaSignUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.keba.lamp.online.properties.OnlineFilingProperties;
import com.keba.lamp.online.service.OnlineCaseApiFacade;
import com.keba.lamp.online.vo.OnlineCaseParamVO;
import com.keba.lamp.online.vo.OnlineTaskParamVO;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.LinkedHashMap;
import java.util.Map;


@Slf4j
@Service
@RequiredArgsConstructor
public class OnlineCaseApiFacadeImpl implements OnlineCaseApiFacade {

    private final OnlineFilingProperties onlineFilingProperties;

    @Override
    public void pushTask(Long bizId, String taskName, OnlineCaseParamVO onlineCaseParamVO) {
        OnlineTaskParamVO onlineTaskParamVO = new OnlineTaskParamVO()
                .setTaskType("自动立案").setName(taskName).setTitle(taskName).setBizId(bizId)
                .setParams(onlineCaseParamVO);

        // 请求参数
        Map<String, Object> paramMap = new LinkedHashMap<>();
        paramMap.put("userId", onlineFilingProperties.getUserId());
        // 补全 timestamp、nonce、sign 参数，并序列化为 kv 字符串
        String paramStr = SaSignUtil.addSignParamsAndJoin(paramMap);
        SaSignUtil.addSignParamsAndJoin(paramMap);
        String url = onlineFilingProperties.getUrl() + "/api/v1/task/push";
        url += "?" + paramStr;
        String result = HttpUtil.post(url, JSONUtil.toJsonStr(onlineCaseParamVO));
        log.info("请求push接口返回：{}", result);


    }
}
